package com.cskaoyan.market.admin.controller;

import com.cskaoyan.market.admin.service.MarketAdminCommentService;
import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
    *@Author：檀志浩
    *@Package：com.cskaoyan.market.controller
    *@Project：Project1
    *@name：AdminCommentController
    *@Date：2023/5/10  10:57
    *@Filename：AdminCommentController
    */
@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController{
    @Autowired
    private MarketAdminCommentService commentService;


    @PostMapping("/delete")
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.parseRequestBody(req);
        int result = commentService.deleteComment(requestBody);
        if (result == 200){
            Object o = ResponseUtil.ok();
            String s = JacksonUtil.writeValueAsString(o);
            resp.getWriter().println(s);

        }else {
            Object o = ResponseUtil.fail();
            resp.getWriter().println(JacksonUtil.writeValueAsString(o));
        }
    }


    @GetMapping("/list")
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketComment> commentList = commentService.getCommentList(page, limit, sort, order);

        Object o = ResponseUtil.okList(commentList);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().println(s);
    }
}
